﻿using B.S.DataBase.MyProject.Domain.Infrastructures;
using B.S.DataBase.MyProject.Domain.SmartHealthcareZSJ;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.CarouselimageCommand;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.CarouselimageCommandHandler
{
    /// <summary>
    /// 查询轮播图命令处理类
    /// </summary>
    public class CarouselimageQueryCommandHandler : IRequestHandler<CarouselimageQueryCommand, ApiPaging<Carouselimage>>
    {

        /// <summary>
        /// sugar
        /// </summary>
        private readonly SqlSugarContext sugar;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sugar"></param>
        public CarouselimageQueryCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiPaging<Carouselimage>> Handle(CarouselimageQueryCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<Carouselimage> result = new ApiPaging<Carouselimage>();
            try
            {
                var list = sugar.db.Queryable<Carouselimage>().Where(x => x.isDeleteId == 0);

                if (!string.IsNullOrEmpty(request.CarouselTitle))
                {
                    list = list.Where(x => x.CarouselTitle.Contains(request.CarouselTitle));
                }
                if(request.CarouseId!=null)
                {
                    list=list.Where( x=>x.CarouseId == request.CarouseId);
                }

                // 时间范围查询
                if (request.BeginTime.HasValue)
                {
                    list = list.Where(x => x.CreateTime >= request.BeginTime.Value);
                }
                if (request.EndTime.HasValue)
                {
                    list = list.Where(x => x.CreateTime <= request.EndTime.Value);
                }

                list = list.OrderByDescending(x => x.Sorting)
                           .Skip((request.PageIndex - 1) * request.PageSize)
                           .Take(request.PageSize);

                int TotalCount = list.Count();
                int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
                result.list = list.ToList();
                result.TotalCount = TotalCount;
                result.TotalPage = TotalPage;
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"添加团队信息时发生异常: {ex.Message}");
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
